﻿Imports MyDLL

Public Class Form3

    Dim Nav As New MyDLL.RecordNav

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim C As String = TextBox7.Text

        If CheckBox1.Checked Then
            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & "<Passwords>" & vbCr _
                                      & "<Type>Passwords</Type>" & vbCr
        End If

        If CheckBox2.Checked Then
            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & _
            "<FlowDocument xmlns=" & C & "http://schemas.microsoft.com/winfx/2006/xaml/presentation" & C & ">" & vbCr
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Count As String = TextBox6.Text

        If CheckBox1.Checked Then
            Dim Counter As XElement = _
            <Counter>
                <Count><%= Count %></Count>
            </Counter>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Counter.ToString & vbCr & "</Passwords>"
        End If

        If CheckBox2.Checked Then
            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & "</FlowDocument>"
        End If
    End Sub

    Private Sub WriteData()
        Dim ID As Integer = TextBox6.Text
        Dim Service As String = TextBox1.Text
        Dim Username As String = TextBox2.Text
        Dim Password As String = TextBox3.Text

        If CheckBox3.Checked Then
            ID = ID + 1
            TextBox6.Text = ID
        End If

        If CheckBox1.Checked Then
            Dim Servicexml As XElement = _
            <Service>
                <Site id=<%= ID %>><%= Service %></Site>
                <Username id=<%= ID %>><%= Username %></Username>
                <Password id=<%= ID %>><%= Password %></Password>
            </Service>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Servicexml.ToString & vbCr

        End If

        If CheckBox2.Checked Then

            Dim Servicexaml As XElement = _
            <List FontFamily="Arial" FontSize="11pt">
                <ListItem><Paragraph>ID:<%= ID %></Paragraph></ListItem>
                <ListItem><Paragraph>Service\Site:<%= Service %></Paragraph></ListItem>
                <ListItem><Paragraph>Username:<%= Username %></Paragraph></ListItem>
                <ListItem><Paragraph>Password:<%= Password %></Paragraph></ListItem>
            </List>

            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & Servicexaml.ToString & vbCr
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WriteData()
    End Sub

    Private Sub LoadData()
        Try
            Me.Text = "Password Database" & " - " & Form1.TextBox3.Text

            Dim ID As String = TextBox6.Text

            Dim Passwords As XElement = XElement.Load(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

            Dim Service As XElement = _
                <Root>
                    <%= From el In Passwords.<Service>.<Site> _
                        Where el.Attribute("id") = ID _
                        Select el %>
                </Root>
            TextBox1.Text = Service

            Dim Username As XElement = _
        <Root>
            <%= From el In Passwords.<Service>.<Username> _
                Where el.Attribute("id") = ID _
                Select el %>
        </Root>
            TextBox2.Text = Username

            Dim Password As XElement = _
        <Root>
            <%= From el In Passwords.<Service>.<Password> _
                Where el.Attribute("id") = ID _
                Select el %>
        </Root>
            TextBox3.Text = Password

            If CheckBox4.Checked Then
                TextBox3.Text = "********"
            End If

            Dim Count As XElement = _
       <Root>
           <%= From el In Passwords.<Counter>.<Count> _
               Select el %>
       </Root>

            Dim Counter As String = Count

            Label7.Text = "There are " & Counter & " record(s) in the XML file"
        Catch ex As Exception
            MsgBox("No file has been loaded.")
        End Try

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        LoadData()
    End Sub

    Sub Transcribe()
        Try
            Do Until TextBox6.Text = TextBox8.Text
                CheckBox3.CheckState = CheckState.Unchecked
                Dim ID As Integer = TextBox6.Text
                ID = ID + 1
                TextBox6.Text = ID
                LoadData()
                WriteData()
            Loop
        Catch ex As Exception
            MsgBox("Choose a proper range.")
        End Try
    End Sub
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Transcribe()
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Nav.GoToFirst(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Nav.PrevRec(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        Nav.NextRec(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        Nav.GoToLast(TextBox6, Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

        LoadData()
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Try
            Form13.Show()

            CheckBox3.CheckState = CheckState.Unchecked
            TextBox6.Text = 0


            Dim ID As Integer = TextBox6.Text
            Dim XmlFile As XElement = XElement.Load(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

            Dim Counter As XElement = _
            <Root>
                <%= From el In XmlFile.<Counter>.<Count> _
                    Select el %>
            </Root>
            Dim Count As Integer = Counter

            Dim C As String = TextBox7.Text

            Form13.RichTextBox1.Text = Form13.RichTextBox1.Text & "<Passwords>" & vbCr _
                                      & "<Type>Passwords</Type>" & vbCr

            Do Until ID = Count
                ID = ID + 1
                TextBox6.Text = ID

                LoadData()

                Dim Service As String = TextBox1.Text
                Dim Username As String = TextBox2.Text
                Dim Password As String = TextBox3.Text

                Dim Servicexml As XElement = _
                 <Service id=<%= ID %>>
                     <Site><%= Service %></Site>
                     <Username><%= Username %></Username>
                     <Password><%= Password %></Password>
                 </Service>

                Form13.RichTextBox1.Text = Form13.RichTextBox1.Text & Servicexml.ToString & vbCr

            Loop

            Dim LastRec As String = TextBox6.Text

            If CheckBox1.Checked Then
                Dim Counter2 As XElement = _
                <Counter>
                    <Count><%= LastRec %></Count>
                </Counter>

                Form13.RichTextBox1.Text = Form13.RichTextBox1.Text & Counter2.ToString & vbCr & "</Passwords>"
            End If

        Catch ex As Exception
            MsgBox("Processing Failed!")
        End Try
    End Sub

    Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
        CheckBox3.CheckState = CheckState.Unchecked

        If CheckBox1.Checked Then

            Dim xmlfile As XElement = XElement.Load(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

            Dim count As XElement = <root>
                                        <%= From el In xmlfile.<Counter>.<Count> _
                                            Select el %>
                                    </root>
            Dim lastrec As String = count

            Form1.RichTextBox1.Clear()

            TextBox6.Text = "0"

            LoadData()

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & "<Passwords>" & vbCr _
                                      & "<Type>Passwords</Type>" & vbCr


            TextBox8.Text = lastrec

            Transcribe()

            TextBox6.Text = CStr(CInt(TextBox6.Text) + 1)
            LoadData()
            WriteData()

            Dim LatestRec As String = TextBox6.Text

            Dim Counter2 As XElement = _
            <Counter>
                <Count><%= LatestRec %></Count>
            </Counter>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Counter2.ToString & vbCr & "</Passwords>"

            Form1.Save()
            Form1.Open()

            Nav.GoToLast(TextBox6, Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

            LoadData()
        End If
    End Sub

    Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
        CheckBox3.CheckState = CheckState.Unchecked

        If CheckBox1.Checked Then

            Dim xmlfile As XElement = XElement.Load(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

            Dim count As XElement = <root>
                                        <%= From el In xmlfile.<Counter>.<Count> _
                                            Select el %>
                                    </root>
            Dim lastrec As String = count

            Form1.RichTextBox1.Clear()


            WriteData()
            Dim id As Integer = CInt(TextBox6.Text)


            My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO\temp.txt", Form1.RichTextBox1.Text, False)

            Form1.RichTextBox1.Clear()

            TextBox6.Text = "0"

            LoadData()

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & "<Passwords>" & vbCr _
                                          & "<Type>Passwords</Type>" & vbCr


            TextBox8.Text = CStr(id - 1)

            Transcribe()

            Dim data As String = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO\temp.txt")
            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & vbCr & data

            TextBox6.Text = CStr(id)
            TextBox8.Text = lastrec

            Transcribe()

            Dim Counter2 As XElement = _
            <Counter>
                <Count><%= lastrec %></Count>
            </Counter>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Counter2.ToString & vbCr & "</Passwords>"

            Form1.Save()
            Form1.Open()

            TextBox6.Text = CStr(id)
            LoadData()

        End If
    End Sub
End Class